#TODO 导包
import random
import time
import allure
import pytest
from PO.base.get_driver import GetDriver
from PO.page.houtai.page_ht_ProductList import PagehtProductList
from conftest import ht_login_url, BASE_DIR
from utils.decorator import decorator
from utils.logger import Log


#TODO 新建测试类
@allure.feature('后台商品管理')
class TestAddproduct():
    product_name=None
    #TODO 初始化
    def setup_method(self):
        #初始化日志模块
        self.log = Log("后台添加商品用例").get_logger()
        #TODO 获取driver
        self.driver=GetDriver().get_driver(ht_login_url)
        #TODO 获取商品管理页面对象
        self.product_manage = PagehtProductList(self.driver)
        with allure.step("前置条件：后台管理员：admin"):
            login_data=("admin",'123456','8888')
            self.product_manage.base_ht_login(*login_data)
            self.log.info("后台登录测试账号--{}".format(login_data))
            #TODO allure截图
            self.product_manage.base_get_allure_image()

    #TODO 结束方法
    def teardown_method(self):
        with allure.step("测试步骤一：点击商城tab菜单"):
            self.product_manage.page_ht_click_shop_tab()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤二：进入iframe框架"):
            self.product_manage.page_ht_to_iframe2()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤三：点击搜索框，输入商品名称"):
            key_word = TestAddproduct.product_name
            self.log.info("后台搜索框输入的商品名称--{}".format(key_word))
            self.product_manage.page_ht_input_key_word(key_word)
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤四：点击搜索按钮"):
            self.product_manage.page_ht_click_search_btn()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤五：勾选需要删除的商品"):
            time.sleep(5)
            self.product_manage.page_ht_click_check_product()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤六：点击删除按钮"):
            self.product_manage.page_ht_click_del_btn()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤七：点击确定按钮"):
            self.product_manage.page_ht_click_del_confirm_btn()
            self.product_manage.base_get_allure_image()

        self.driver=GetDriver().quit_driver()

    @decorator
    @pytest.mark.run(order=0)
    @allure.story('后台商品管理')
    def test_Addproduct(self):
        with allure.step("测试步骤一：点击商城tab菜单"):
            self.product_manage.page_ht_click_shop_tab()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤二：进入iframe框架"):
            self.product_manage.page_ht_to_iframe2()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤三：点击添加商品"):
            self.product_manage.page_ht_click_add_product()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤四：输入商品名称"):
            name = '自动化商品_{}'.format(random.randint(1, 99))
            # TODO 获取商品名称
            TestAddproduct.product_name=name
            self.log.info("新增的商品名称--{}".format(name))
            self.product_manage.page_ht_input_product_name(name)
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤五：输入商品简介"):
            self.product_manage.page_ht_input_product_remark()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤六：输入SPU"):
            self.product_manage.page_ht_input__product_SPU()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤七：输入SKU"):
            self.product_manage.page_ht_input__product_SKU()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤八：选择商品分类"):
            # TODO 选择商品一级分类
            self.product_manage.page_ht_select_product_category01()
            # TODO 选择商品二级分类
            self.product_manage.page_ht_select_product_category02()
            # TODO 选择商品三级分类
            self.product_manage.page_ht_select_product_category03()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤九：选择商品品牌"):
            self.product_manage.page_ht_select_product_brand()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤十：输入商品售价，市场价，成本价"):
            # TODO 输入商品售价
            self.product_manage.page_ht_input_product_shop_price()
            # TODO 输入市场价
            self.product_manage.page_ht_input_product_market_price()
            # TODO 输入成本价
            self.product_manage.page_ht_input_product_cost_price()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤十一：输入商品佣金"):
            self.product_manage.page_ht_input_product_commission()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤十二：上传商品图片"):
            filepath = BASE_DIR + r"/image/1.jpg"
            # TODO 点击上传图片按钮
            self.product_manage.page_ht__upload_product_image_btn()
            # TODO 进入上传图片iframe框架
            self.product_manage.page_ht_to_iframe5()
            # TODO 上传图片路径
            self.product_manage.page_ht_upload_product_image(filepath)
            # TODO 等待图片上传成功
            self.product_manage.page_ht_upload_success()
            # TODO 点击确认上传按钮
            self.product_manage.page_ht_click_upload_success_btn()
            # TODO 退出上传图片iframe框架
            self.driver.switch_to.default_content()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤十三：输入商品重量"):
            #TODO 进入iframe框架
            self.product_manage.page_ht_to_iframe2()
            # TODO 输入商品重量
            self.product_manage.page_ht_input_product_weight()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤十四：输入商品库存"):
            self.product_manage.page_ht_input_product_stock()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤十五：输入商品关键字"):
            self.product_manage.page_ht_input_product_keyword()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤十六：输入商品详情描述"):
            # TODO 进入输入商品描述iframe框架
            self.product_manage.page_ht_to_iframe6()
            # TODO 输入商品详情描述
            self.product_manage.page_ht_input_product_detail()
            # TODO 退出输入商品描述iframe框架
            self.driver.switch_to.default_content()
            self.product_manage.base_get_allure_image()

        with allure.step("测试步骤十四：点击提交按钮"):
            # TODO 进入iframe框架
            self.product_manage.page_ht_to_iframe2()
            # TODO 点击提交按钮
            self.product_manage.page_ht_click_confirm_btn()
            # TODO 退出iframe框架
            self.driver.switch_to.default_content()
            self.product_manage.base_get_allure_image()


